package com.lzx.iteam;

import android.app.Activity;
import android.content.Intent;
import android.net.wifi.WifiInfo;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.view.View;
import android.widget.*;
import com.lzx.iteam.adapter.AttendanceWifiAdapter;
import com.lzx.iteam.bean.WifiMsgData;
import com.lzx.iteam.util.NetworkUtil;
import com.lzx.iteam.util.StringUtil;
import com.lzx.iteam.widget.MyListView;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;


/**
 * Created by Megan on 16/7/29.
 */
public class AttendanceWifiSettingActivity extends Activity implements View.OnClickListener {

    private LinearLayout mLayoutBack;
    private TextView mTvTitle;
    private TextView mTvRight;
    private TextView mTvWifiName;
    private TextView mTvWifiStatus;
    private ImageView mIvAdd;
    private LinearLayout mLlCurrentWifi;
    private LinearLayout mLlSetWifi;
    private LinearLayout mLlNoData;
    private RelativeLayout mRlOnly;
    private ToggleButton mTbOnly;
    private int mIfOnly;
    private MyListView mLvList;
    private String mWifiMsg;
    private String mDeviation;
    private WifiInfo wifiInfo;
    private ArrayList<WifiMsgData> wifiMsgDatas = new ArrayList<WifiMsgData>();

    private AttendanceWifiAdapter adapter;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.attendance_wifi_setting_layout);
        initView();
        setData();
    }

    private void initView() {
        mLayoutBack = (LinearLayout) this.findViewById(R.id.title_ll_back);
        mTvTitle = (TextView) this.findViewById(R.id.title_tv_center);
        mTvRight = (TextView) this.findViewById(R.id.title_tv_right);
        mTvWifiName = (TextView) this.findViewById(R.id.tv_attendance_wifi_setting_name);
        mTvWifiStatus = (TextView) this.findViewById(R.id.tv_attendance_wifi_setting_state);
        mIvAdd = (ImageView) this.findViewById(R.id.iv_attendance_wifi_setting_add);
        mLvList = (MyListView) this.findViewById(R.id.lv_attendance_wifi_setting_in_list);
        mLlCurrentWifi = (LinearLayout) this.findViewById(R.id.ll_wifi_setting_current);
        mLlSetWifi = (LinearLayout) this.findViewById(R.id.ll_wifi_setting_list);
        mLlNoData = (LinearLayout) this.findViewById(R.id.ll_wifi_setting_no_data);
        mRlOnly = (RelativeLayout) this.findViewById(R.id.rl_wifi_setting_only);
        mTbOnly = (ToggleButton) this.findViewById(R.id.tb_wifi_setting_only);
        mTvRight.setVisibility(View.VISIBLE);
        mLayoutBack.setOnClickListener(this);
        mTvRight.setOnClickListener(this);
        mIvAdd.setOnClickListener(this);
        mRlOnly.setOnClickListener(this);

    }

    private void setData() {
        Intent intent = getIntent();
        mWifiMsg = intent.getStringExtra("wifiMsg");
        mDeviation = intent.getStringExtra("deviation");
        mTvTitle.setText("考勤WiFi");
        mTvRight.setText("保存");

        wifiInfo = NetworkUtil.getWifiInfo(this);
        if (StringUtil.isEmpty(wifiInfo.getBSSID())) {
            mLlCurrentWifi.setVisibility(View.GONE);
        } else {
            mLlCurrentWifi.setVisibility(View.VISIBLE);
            mTvWifiName.setText(wifiInfo.getSSID());
        }
        adapter = new AttendanceWifiAdapter(this);
        mLvList.setAdapter(adapter);
        try {
            if (!StringUtil.isEmpty(mWifiMsg)) {
                JSONArray jsonArray = new JSONArray(mWifiMsg);
                if (jsonArray.length() > 0) {
                    boolean has = false;
                    for (int i = 0; i < jsonArray.length(); i++) {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);
                        WifiMsgData wifiMsgData = new WifiMsgData();
                        wifiMsgData.setWifiName(jsonObject.getString("wifi_name"));
                        wifiMsgData.setWifiMac(jsonObject.getString("wifi_mac"));
                        wifiMsgDatas.add(wifiMsgData);
                        if (jsonObject.getString("wifi_mac").equals(wifiInfo.getBSSID())) {
                            has = true;
                        }
                    }
                    if (has) {
                        mIvAdd.setVisibility(View.GONE);
                    } else {
                        mIvAdd.setVisibility(View.VISIBLE);
                    }

                } else {
                    WifiMsgData wifiMsgData = new WifiMsgData();
                    wifiMsgData.setWifiName(wifiInfo.getSSID());
                    wifiMsgData.setWifiMac(wifiInfo.getBSSID());
                    wifiMsgDatas.add(wifiMsgData);
                    adapter.bindData(wifiMsgDatas);
                    mIvAdd.setVisibility(View.GONE);
                }
            } else {
                WifiMsgData wifiMsgData = new WifiMsgData();
                wifiMsgData.setWifiName(wifiInfo.getSSID());
                wifiMsgData.setWifiMac(wifiInfo.getBSSID());
                wifiMsgDatas.add(wifiMsgData);
                adapter.bindData(wifiMsgDatas);
                mIvAdd.setVisibility(View.GONE);
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        adapter.bindData(wifiMsgDatas);

        if (mLlCurrentWifi.getVisibility() == View.GONE && wifiMsgDatas.size() == 0) {
            mLlNoData.setVisibility(View.VISIBLE);
        } else {
            mLlNoData.setVisibility(View.GONE);

        }
        if ("0".equals(mDeviation)) {
            setIfOnly(1);
        } else {
            setIfOnly(0);
        }

    }


    private void setIfOnly(int flag) {
        if (flag == 0) {
            mTbOnly.setChecked(false);
        } else if (flag == 1) {
            mTbOnly.setChecked(true);
        }
    }

    private int getIfOnly() {
        if (mTbOnly.isChecked()) {
            mIfOnly = 1;
        } else {
            mIfOnly = 0;
        }
        return mIfOnly;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_ll_back:
                finish();
                break;
            case R.id.title_tv_right:
                JSONArray jsonArray = new JSONArray();
                try {
                    for (int i = 0; i < wifiMsgDatas.size(); i++) {
                        JSONObject jsonObject = new JSONObject();
                        jsonObject.put("wifi_mac", wifiMsgDatas.get(i).getWifiMac());
                        jsonObject.put("wifi_name", wifiMsgDatas.get(i).getWifiName());
                        jsonArray.put(jsonObject);
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
                Intent intent = new Intent();
                intent.putExtra("wifiMsg", jsonArray.toString());
                intent.putExtra("wifiOnly", getIfOnly());
                setResult(RESULT_OK, intent);
                finish();

                break;
            case R.id.iv_attendance_wifi_setting_add:
                WifiMsgData wifiMsgData = new WifiMsgData();
                wifiMsgData.setWifiName(wifiInfo.getSSID());
                wifiMsgData.setWifiMac(wifiInfo.getBSSID());
                wifiMsgDatas.add(wifiMsgData);
                adapter.bindData(wifiMsgDatas);
                mIvAdd.setVisibility(View.GONE);
                break;
            case R.id.rl_wifi_setting_only:
                mTbOnly.toggle();
                break;
        }
    }

    public void deleteData(WifiMsgData wifiMsgData) {
        wifiMsgDatas.remove(wifiMsgData);
        adapter.bindData(wifiMsgDatas);
        if (wifiMsgData.getWifiMac().equals(wifiInfo.getBSSID())) {
            mIvAdd.setVisibility(View.VISIBLE);
        }
    }
}
